const express = require('express')
const path = require('path')

const app = new express()
const port = 3003

app.all('*', (req, res, next) => {
  res.setHeader('Access-Control-Allow-Origin', req.get('Origin') || '*')
  res.setHeader(
    'Access-Control-Allow-Headers',
    'Origin, Content-Type, Cache-Control, Content-Length, Authorization, Accept, X-Requested-With',
  )
  res.setHeader('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS')
  next()
})

app.use(express.static(path.join(__dirname, '../build')))
app.use(express.static(path.join(__dirname, './auth0')))
app.use('*', (req, res) => {
  res.sendFile(path.join(__dirname, '../build', 'index.html'))
})

app.listen(port, (err) => {
  if (err) {
    console.error(err)
  } else {
    console.info(`the express server has been listened at port: ${port}`)
  }
})
